package com.example.recuitactivity.viewTables;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.recuitactivity.helper.DbOpenHelper;
import com.example.recuitactivity.R;

import java.util.Arrays;
import java.util.List;

public class ViewEnterprisesTableActivity extends AppCompatActivity {
    private TableLayout table;
    private Button btn_next,btn_last;
    private DbOpenHelper dbOpenHelper;
    private List<List<String>> list;
    private int current_count=0,currentItemStart=0,nextItemStart=0;
    private int itemInPage[]=new int[20],pageCount=0,pageIndex=0;
    private boolean isFilled=false;//用于标记当前页是否被填满

    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.btn_enterprise_next:
                    if(isFilled){
                        pageCount=Math.max(pageCount,pageIndex+1);
                        itemInPage[pageIndex++]=current_count;
                        deleteRowItem();
                        initTable();
                    }
                    break;
                case R.id.btn_enterprise_last:
                    if(pageIndex>0){
                        nextItemStart=currentItemStart-itemInPage[--pageIndex];
                        deleteRowItem();
                        initTable();
                    }
                    break;
            }
        }
    };

    public void initUI(){
        dbOpenHelper=new DbOpenHelper(ViewEnterprisesTableActivity.this);
        list=dbOpenHelper.getAllEnterprises();

        btn_next=(Button) findViewById(R.id.btn_enterprise_next);
        btn_last=(Button) findViewById(R.id.btn_enterprise_last) ;
        table=(TableLayout) findViewById(R.id.tl_enterprises);
        table.setStretchAllColumns(true);

        btn_next.setOnClickListener(listener);
        btn_last.setOnClickListener(listener);
    }

    public  void initTable(){
        int w= View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h= View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

        currentItemStart=nextItemStart;
        for(int i=nextItemStart;i<list.size();i++){
            TableRow tableRow=new TableRow(ViewEnterprisesTableActivity.this);
            long eid=Long.valueOf(list.get(i).get(0));
            for(String str:list.get(i)){
                TextView textView=new TextView(ViewEnterprisesTableActivity.this);
                textView.setBackgroundResource(R.drawable.tv_shape);
                textView.setGravity(Gravity.CENTER);
                textView.setTextSize(20);
                textView.setText(str);
                textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT));
                tableRow.addView(textView);
            }

            Button btn_delete=new Button(ViewEnterprisesTableActivity.this);
            btn_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    new AlertDialog.Builder(ViewEnterprisesTableActivity.this)
                            .setTitle("提示")
                            .setMessage("确定删除该用户吗？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    dbOpenHelper.deleteEnterpriseById(eid);
                                    list= dbOpenHelper.getAllEnterprises();
                                    //重置各参数
                                    currentItemStart=0;
                                    nextItemStart=0;
                                    pageCount=0;
                                    pageIndex=0;
                                    Arrays.fill(itemInPage,0);
                                    isFilled=false;
                                    deleteRowItem();
                                    initTable();
                                    Toast.makeText(ViewEnterprisesTableActivity.this,"已删除",Toast.LENGTH_SHORT).show();
                                }
                            })
                            .setNegativeButton("取消",null)
                            .show();
                }
            });
            btn_delete.setText("注销");
            btn_delete.setTextColor(Color.RED);
            btn_delete.setTextSize(20);
            btn_delete.setBackgroundResource(R.drawable.tv_shape);
            btn_delete.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT));
            tableRow.addView(btn_delete);

            table.addView(tableRow,new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
            table.measure(w,h);
            current_count++;//当前页row计数
            if(table.getMeasuredHeight()>getWindowManager().getDefaultDisplay().getHeight()*0.75){
                isFilled=true;//标记为当前页被填满
                nextItemStart=i+1;//下一页开始项
                break;
            }
            isFilled=false;//当前页未被填满
        }


    }

    public void deleteRowItem(){
        for(int i=0;i<current_count;i++){
            table.removeViewAt(table.getChildCount()-1);
        }
        current_count=0;//当前页count清零
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_enterprises_table);
        ActionBar actionBar=getSupportActionBar();
        actionBar.setTitle("enterprises表");
        actionBar.setDisplayHomeAsUpEnabled(true);
        initUI();
        initTable();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return false;
    }
}